﻿using CasignDesignGA.Casing;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CasignDesignGA.Engine.Selection
{
    class EliteRouletteSelectionAlgorithm : ISelectionAlgorithm
    {
        private static EliteRouletteSelectionAlgorithm instance;
        private EliteRouletteSelectionAlgorithm()
        {           
        }
        public static EliteRouletteSelectionAlgorithm Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new EliteRouletteSelectionAlgorithm();
                }
                return instance;
            }
        }
        public Population.Population selectPopulation(Population.Population individuals, int nIndividuals)
        {
            Population.Population selectedIndividuals = new Population.Population();

            int nRoulette = nIndividuals -1 ;
            
            Random rand = new Random(DateTime.Now.Millisecond);
            
            selectedIndividuals.Add(individuals.getBestIndividual());
            /*Roulette part*/
            for (int i = 0; i < nRoulette; i++)
            {
                double randIndex = rand.NextDouble() * individuals.count();
                int index = Convert.ToInt32(randIndex);
                
                ISelectable selectedRouletteIndividual = individuals.getIndividualIndex(index);
                if (selectedRouletteIndividual != null && !selectedIndividuals.Individuals.Contains(selectedRouletteIndividual))
                {
                    selectedIndividuals.Add(selectedRouletteIndividual);
                }

            }

           

            

            return selectedIndividuals;
        }
    }
}
